package com.dlc.yunguang.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.yunguang.net.bean.HotelRfidBean;
import com.dlc.yunguang.net.bean.SaveRfidBean;
import com.dlc.yunguang.net.bean.DaoMaster;
import com.dlc.yunguang.net.bean.DaoSession;
import com.dlc.yunguang.net.bean.HotelRfidBeanDao;
import com.dlc.yunguang.net.bean.SaveRfidBeanDao;
import java.util.List;
import org.greenrobot.greendao.query.QueryBuilder;

/**
 * Created by Administrator on 2017-07-04.
 */

public class DBManager {
    private final static String dbName = "test_db";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;

    public DBManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }
    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }
    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }
    /**
     * 插入一条记录
     *
     * @param user
     */
    public void insertLabel(SaveRfidBean user) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SaveRfidBeanDao userDao = daoSession.getSaveRfidBeanDao();
        userDao.insert(user);
    }

    /**
     * 删除所有记录
     *
     * @param
     */
    public void deleteLabelAll() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SaveRfidBeanDao userDao = daoSession.getSaveRfidBeanDao();
        //userDao.delete(user);
        userDao.deleteAll();
    }
    /**
     * 查询用户列表
     */
    public List<SaveRfidBean> queryLabelList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SaveRfidBeanDao userDao = daoSession.getSaveRfidBeanDao();
        QueryBuilder<SaveRfidBean> qb = userDao.queryBuilder();
        List<SaveRfidBean> list = qb.list();
        return list;
    }


    /**
     * 插入一条记录
     *
     * @param user
     */
    public void insertHotelLabel(HotelRfidBean user) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        HotelRfidBeanDao userDao = daoSession.getHotelRfidBeanDao();
        userDao.insert(user);
    }
    //
    //public void insertNameBean(NameBean nameBean) {
    //    DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
    //    DaoSession daoSession = daoMaster.newSession();
    //    NameBeanDao userDao = daoSession.getNameBeanDao();
    //    userDao.insert(nameBean);
    //}
    //
    /**
     * 删除所有记录
     *
     * @param
     */
    public void deleteLabelAllHotel() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        HotelRfidBeanDao userDao = daoSession.getHotelRfidBeanDao();
        //userDao.delete(user);
        userDao.deleteAll();
    }
    public boolean deleteLabel() {
        boolean isdelete = false;
        long timeStamp = System.currentTimeMillis();
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        HotelRfidBeanDao userDao = daoSession.getHotelRfidBeanDao();
        QueryBuilder<HotelRfidBean> qb = userDao.queryBuilder();
        List<HotelRfidBean> list = qb.list();
        //userDao.delete(user);
        // LogPlus.e("timeStamp="+timeStamp+"");
        for (int i = 0;i < list.size();i++){
            HotelRfidBean bean = list.get(i);
            //  LogPlus.e("Time="+ bean.getTime()+"");
            // LogPlus.e("差值="+ (timeStamp - bean.getTime())+"");
            if(timeStamp - bean.getTime() > (12*60*60*1000)){
                userDao.delete(bean);
                LogPlus.e("自动删除成功");
                isdelete = true;
            }
        }
        return isdelete;
    }
    //14:50
    /**
     * 查询用户列表
     */
    public List<HotelRfidBean> queryLabelListHotel() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        HotelRfidBeanDao userDao = daoSession.getHotelRfidBeanDao();
        QueryBuilder<HotelRfidBean> qb = userDao.queryBuilder();
        List<HotelRfidBean> list = qb.list();
        return list;
    }



}
